using System.Collections.Generic;
using UnityEngine;

namespace NL.Advanced
{


    public interface ITargetCollecter
    {
        // float Range { get; set; }
        List<ICombatOwner> GetTop(int count);
        List<ICombatOwner> All { get; }

        ICombatOwner GetTopOne(TargetCollectTopmodel model = TargetCollectTopmodel.None);

    }


    public class TargetCollecter : TargetCollectorCustom, ITargetCollecter
    {


        // float ITargetCollecter.Range { get=>Range; set =>Range=value; }
        List<ICombatOwner> ITargetCollecter.GetTop(int count) => GetTop(count);
        List<ICombatOwner> ITargetCollecter.All => All;
        ICombatOwner ITargetCollecter.GetTopOne(TargetCollectTopmodel model) => GetTopOne(model);
        protected override void Awake()
        {
            gameObject.layer = LayerMask.NameToLayer("Radar");

            _Collider = gameObject.GetComponent<Collider>();
            if (_Collider != null)
            {
                _Collider.isTrigger = true;
            }

            var rigi = gameObject.GETOrAddComponent<Rigidbody>();

            if (rigi != null)
            {
                rigi.useGravity = false;
                rigi.isKinematic = true;
            }

            CombatOwner = GetComponentInParent<ICombatOwner>();
        }

        public ICombatOwner GetTopOne(TargetCollectTopmodel model = TargetCollectTopmodel.None)
        {

            return _GetTopest();

        }

        ICombatOwner _GetTopest()
        {

            if (_All == null || _All.Count < 1) return null;

            Sort();

            do
            {
                var top = _All[0];
                if (top == null || !top.Living || top.gameObject == null || !top.gameObject.activeInHierarchy)
                {
                    _All.RemoveAt(0);
                }
                else
                {
                    return top;
                }

            } while (_All.Count > 0);


            return null;



        }
        public List<ICombatOwner> GetTop(int count)
        {
            Sort();
            List<ICombatOwner> result = new List<ICombatOwner>();
            List<ICombatOwner> bads = new List<ICombatOwner>();
            foreach (var a in _All)
            {
                if (a == null || !a.Living || a.gameObject == null || !a.gameObject.activeInHierarchy)
                {
                    bads.Add(a);
                }
                else
                {
                    result.Add(a);
                    if (result.Count >= count)
                        break;
                }
            }

            foreach (var b in bads)
            {
                _All.Remove(b);
            }

            return result;
        }

        protected virtual void OnTriggerEnter(Collider other)
        {
            Add(other);
        }

        protected virtual void OnTriggerExit(Collider other)
        {
            var cmo = other.GetComponentInParent<ICombatOwner>();

            if (cmo == null) return;

            if (_All.Contains(cmo))
            {
                _All.Remove(cmo);
            }
        }
    }

}
